package com.heima.cn;

import com.heima.com.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/ListServletStudent")
public class ListServletStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //将本地磁盘中的数据一条一条的读取出来
        //封装成学生对象
        //添加值集合中
        ArrayList<Student> lists = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("d:\\stu.txt"));
        String line ;
        while ((line= br.readLine())!=null){
            String[] arr = line.split(",");
            Student student = new Student();
            student.setName(arr[0]);
            student.setAge(Integer.parseInt(arr[1]));
            student.setSoccer(Integer.parseInt(arr[2]));
            lists.add(student);
        }
        br.close();
        //创建响应对象，将list响应给集合
        resp.setContentType("text/html;charset=UTF-8");
        for(Student student: lists){
            resp.getWriter().write(student.getName()+","+student.getAge()+","+student.getSoccer());
            resp.getWriter().write("<br/>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
